package cn.yunhe.util;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

/**
 * Created by Administrator on 2017/9/22.
 */
public class JsonUtil {

    public static JSONArray formatRsToJsonArray(ResultSet rs) {
        JSONArray array = new JSONArray();
        try {
            ResultSetMetaData rsd = rs.getMetaData();
            int num = rsd.getColumnCount();

            while (rs.next()) {
                JSONObject mapOfColValues = new JSONObject();
                for (int i = 1; i <= num; i++) {
                    mapOfColValues.put(rsd.getColumnName(i), rs.getObject(i));
                }
                array.add(mapOfColValues);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return array;
    }
}
